//
//  New FeatureCollectionViewController.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
///注册可重用cell
private let New_FeatureCellID = "New_FeatureCellID"
///设置Item属性
private let New_FeatureCellCount = 4
class New_FeatureCollectionViewController: UICollectionViewController {
    // MARK: - 构造函数
    init() {
        
        let layout = UICollectionViewFlowLayout()
        //设置item属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        //垂直间距
        layout.minimumLineSpacing = 0
        //设置滚动方向
        layout.scrollDirection = .Horizontal
        // 构造函数，完成之后内部属性才会被创建
        
        super.init(collectionViewLayout: layout)
        //设置collectionView的属性
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: New_FeatureCellID)
    }
    
    
    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return New_FeatureCellCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(New_FeatureCellID, forIndexPath: indexPath) as! NewFeatureCell
        
        // cell.backgroundColor = UIColor.whiteColor()
        //根据indexPath创建cell的图片
        cell.imageIndex = indexPath.item
     
        return cell
        
    }
    //滚动结束播放动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //1.计算当前页数
        let page = Int (scrollView.contentOffset.x / scrollView.bounds.width)
      //2.判断是否最后一页
        if page != New_FeatureCellCount - 1 {
        
        return
        }
    //播放动画
      let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        cell.showButtonAnim()
    }
}
//MARK: - 自定义cell
private class NewFeatureCell:UICollectionViewCell {
    /// 图像属性
    private var imageIndex:Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            // 隐藏按钮
            StartButton.hidden = true

        }
        
    }
    /// 显示按钮动画
    private func showButtonAnim() {
        StartButton.hidden = false
        StartButton.transform = CGAffineTransformMakeScale(0, 0)
        StartButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.6,  // 动画时长
            delay: 0,                    // 延时时间
            usingSpringWithDamping: 0.8, // 弹力系数，0~1，越小越弹
            initialSpringVelocity: 10,   // 初始速度，模拟重力加速度
            options: [],                 // 动画选项
            animations: { () -> Void in
                self.StartButton.transform = CGAffineTransformIdentity
                //self.StartButton.layoutSubviews()
            }) { (Bool) -> Void in
                self.StartButton.userInteractionEnabled = true
        }
    }
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///添加控件
    private func setUI() {
        
        addSubview(iconView)
        iconView.frame = bounds
        
        addSubview(StartButton)
        //设置按钮的布局
        StartButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            StartButton.addTarget(self, action: "BtnClick", forControlEvents: .TouchUpInside)
        }
    }
    //MARK: - 注册点击事件
    @objc func BtnClick() {
        //跳转视图
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    //MARK: - 懒加载控件
    //图像
    private var iconView:UIImageView = UIImageView()
    //按钮
    private lazy var StartButton:UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
}